package com.hymall.service.admin;

        import com.github.pagehelper.PageHelper;
        import com.github.pagehelper.PageInfo;
        import com.hymall.bean.*;
        import com.hymall.bean.common.BasePageInfo;
        import com.hymall.bean.common.CommonData;
        import com.hymall.mapper.*;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import java.util.List;

/**
 * @Description 用户管理模块service
 * @Author 徐成喜
 * @Data 2022/7/17 16:39
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    /**
     * @return CommonData<MarketAddress>
     * @description 地址管理，实现收货地址查询
     */
    @Override
    public CommonData<MarketAddress> queryAddress(String name, Integer userId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if ( userId!= null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketAddress> marketUsers = marketAddressMapper.selectByExample(example);

        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketUsers);
        return CommonData.data(pageInfo);

    }


    @Autowired
    MarketCollectMapper marketCollectMapper;

    /**
     * @return CommonData<MarketCollect>
     * @description 地址管理，实现会员收藏查询
     */
    @Override
    public CommonData<MarketCollect> queryCollect(Integer valueId, Integer userId, BasePageInfo info) {

        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();

        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        if ( userId!= null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);

        PageInfo<MarketCollect> pageInfo = new PageInfo<>(marketCollects);
        return CommonData.data(pageInfo);
    }

    /**
     * @return CommonData<MarketFootprint>
     * @description 实现会员足迹查询
     */

    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Override
    public CommonData<MarketFootprint> queryFootprint(Integer goodsId, Integer userId,BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketFootprintExample example = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        if ( userId!= null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(example);
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(marketFootprints);
        return CommonData.data(pageInfo);
    }

    /**
     * @return CommonData<MarketSearchHistory>
     * @description {用queryHistory方法实现了搜索历史记录
     */
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Override
    public CommonData<MarketSearchHistory> queryHistory(String keyword, Integer userId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        if (keyword != null) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId!= null) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(example);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(marketSearchHistories);
        return CommonData.data(pageInfo);

    }

    /**
     * @return CommonData<MarketFeedback>
     * @description 用queryFeedback方法实现了意见反馈
     */

    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;
    @Override
    public CommonData<MarketFeedback> queryFeedback(String username, Integer id, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketFeedbackExample example = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = example.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id!= null) {
            criteria.andIdEqualTo(id);
        }
        List<MarketFeedback> marketFeedbacks = marketFeedbackMapper.selectByExample(example);
        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(marketFeedbacks);
        return CommonData.data(pageInfo);
    }
}
